package com.peng.component;

import com.peng.data.WakCommonData;
import com.peng.event.CzAndCyKeyListener;

import javax.swing.*;
import java.awt.*;

/**
 * @author : pengyf
 * @version : 1.0
 * @createTime : 2022/1/22 13:25
 * @Description : TODO
 */
public class MyJTextArea extends JTextArea {
    private int x;
    private int y;
    private int width;
    private int height;
    private Font defaultFont = WakCommonData.DEFAULTFONT;

    public MyJTextArea(int x, int y, int width, int height) {
        super();
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        init();
    }

    public MyJTextArea(int x, int y, int width, int height, Font defaultFont) {
        super();
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.defaultFont = defaultFont;
        init();
    }

    public void init(){
        setBounds(x,y, width,height);
        setFont(defaultFont);
        setLineWrap(true);//设置自动换行
        getDocument().addUndoableEditListener(WakCommonData.UNDOMANAGER);
        addKeyListener(new CzAndCyKeyListener());
    }

    public JScrollPane scrollJTextArea(){
        // 原理：JTextArea需要添加到JScrollPanel，然后再把JScrollPanel添加到当前窗口，重点是JTextArea添加到JScrollPanel时不要采用add方法，而是需要scrollPane.setViewportView(textArea);这样才能够显示出来
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(x,y, width,height);
        scrollPane.setViewportView(this);
        return scrollPane;
    }

    public JTextArea getJTextArea(){
        return this;
    }

}
